package cn.amaake.controller;

import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.amaake.common.AmaakePage;
import cn.amaake.common.Result;
import cn.amaake.common.model.Class;
import cn.amaake.common.model.Course;
import cn.amaake.common.model.Student;
import cn.amaake.common.model.User;
import cn.amaake.service.CourseService;
import cn.amaake.service.CourseServiceImp;

public class CourseController extends Controller {

	@Inject(CourseServiceImp.class)
	private CourseService courseService;
	
    public void index(){
        render("/views/course/index.html");
    }
    
    public void getCoursePage(){
        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String name = getPara("name");
        Page<Course> coursePage = courseService.getCoursePage(pageNumber, pageSize, name);
        Result result = new Result();
        result.setData(new AmaakePage(coursePage));
        renderJson(result);
    }

    public void showAddCourse(){
        render("/views/course/add.html");
    }

    public void addCourse(){
        String Kcid = getPara("Kcid");
        String Kcname = getPara("Kcname");
        int Periodexpriment = getParaToInt("Periodexpriment");
        int Creidt = getParaToInt("Creidt");
        int Coursetype = getParaToInt("Coursetype");
        String brief = getPara("brief");
        Course course = new Course();
        course.setKcid(Kcid);
        course.setKcname(Kcname);
        course.setPeriodexpriment(Periodexpriment);
        course.setCreidt(Creidt);
        course.setCoursetype(Coursetype);
        course.setBrief(brief);
        boolean boo = course.save();
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("添加成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("添加失败！");
        }
        renderJson(result);
    }

    @Before(Tx.class)
    public void deleteCourse(){
        String ids = getPara("id");
        String[] idss = ids.split(",");
        Result result = new Result();
        boolean boo = false;
        for (String id : idss){
            int idi = Integer.parseInt(id);
            boo = Course.dao.deleteById(idi);
        }
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("删除成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("删除失败！");
        }
        renderJson(result);
    }

    public void showEditCourse(){
        String id = getPara("id");
        setAttr("id",id);
        render("/views/course/edit.html");
    }

    public void getCourse(){
        String id = getPara("id");
        Course course = Course.dao.findById(id);
        Result result = new Result();
        result.setData(course);
        renderJson(result);
    }

    public void editCourse(){
        String id = getPara("id");
        String Kcid = getPara("Kcid");
        String Kcname = getPara("Kcname");
        int Periodexpriment = getParaToInt("Periodexpriment");
        int Creidt = getParaToInt("Creidt");
        int Coursetype = getParaToInt("Coursetype");
        String brief = getPara("brief");
        Result result = new Result();
        Course course = Course.dao.findById(id);
        course.setKcid(Kcid);
        course.setKcname(Kcname);
        course.setPeriodexpriment(Periodexpriment);
        course.setCreidt(Creidt);
        course.setCoursetype(Coursetype);
        course.setBrief(brief);
        boolean boo = course.update();
        if (boo) {
            result.setErrorNo(0);
            result.setErrorInfo("更新成功！");
        } else {
            result.setErrorNo(500);
            result.setErrorInfo("更新失败！");
        }
        renderJson(result);
    }
}
